<template>
  <div class="app-container calendar-list-container" style="margin: 10px">
    <el-form :inline="true" :model="queryDataForm" @keyup.enter.native="getList()"  class="company-wrapper_search" @submit.native.prevent>
      <el-form-item label="字典名称">
        <el-input v-model="queryDataForm.name" placeholder="请输入字典名称"></el-input>
      </el-form-item>
      <el-form-item label="字典类型">
        <el-input v-model="queryDataForm.type" placeholder="请输入字典类型"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getList('noPage')"  type="primary">查询</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button
      class="mtb15"
      @click="add"
      type="primary"
      v-if="this.$can('DIC_ADD')"
    >新增字典</el-button>
    <NbTable
      :tableHeaders="tableHeaders"
      :tableData="tableData"
      :clickData="clickData"
      :headerMap="queryMap"
      operatorFixed="left"
      operatorText
      height="auto"
      @clickRight="clickRight"
      :align="align"
    />
    <!-- 分页 -->
    <el-pagination
      align="right"
      style="padding-top: 20px;margin-bottom:100px;"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="queryDataForm.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryDataForm.size"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 200px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}

.scroll-content {
  background: #fff;
}
</style>

<script>
import NbTable from '../../../components/nb-table'
export default {
  name: 'promotion',
  components: {
    NbTable
  },
  data () {
    return {
      queryDataForm: {
        name: '',
        type: '',
        page: 1,
        size: 10
      },
      isShowError: false,
      dialogVisible: false,
      dataList: [],
      clickData: [],
      totalPage: 0,
      pageSize: 10,
      pageIndex: 1,
      listLoading: false,
      resetDataForm: {
        donePwd: '',
        pwd: ''
      },
      code: '',
      // clickData: [
      //   {
      //     name: '编辑',
      //     type: 'edit'
      //   }, {
      //     name: '删除',
      //     type: 'delete'
      //   }
      // ],
      tableData: [],
      tableHeaders: [
        { headerKey: 'name', headerName: '字典名称' },
        { headerKey: 'type', headerName: '字典类型' },
        { headerKey: 'dicCode', headerName: '字典代码' },
        { headerKey: 'dicValue', headerName: '字典值' },
        { headerKey: 'orderDesc', headerName: '顺序号' },
        { headerKey: 'url', headerName: '链接地址' },
        { headerKey: 'remark', headerName: '备注' }
        // { headerKey: 'companyName', headerName: '公司名称' },
        // { headerKey: 'isEffect', headerName: '是否有效' }
      ],
      align: 'center',
      queryMap: [
        {
          headerKey: 'isEffect',
          headerName: '是否有效',
          fun (row) {
            if (row.isEffect) {
              return '有效'
            } else if (!row.isEffect) {
              return '无效'
            } else {
              return '-'
            }
          }
        }
      ]
    }
  },
  mounted: function () {
    this.clickData = this.$showBtn([
      { btnName: 'DIC_EDIT', name: '编辑', type: 'edit' },
      { btnName: 'DIC_DEL', name: '删除', type: 'delete' }
    ])
    this.getList()
    // document.onkeydown = e => {
    //   var key = window.event.keyCode
    //   if (key == 13) {
    //     this.getList(1)
    //   }
    // }
  },
  methods: {
    onReset () {
      this.queryDataForm.name = ''
      this.queryDataForm.type = ''
      this.getList('noPage')
    },
    clickRight (data) {
      if (data.type === 'edit') {
        this.edit(data.row)
      } else if (data.type === 'delete') {
        this.delUser(data.row.code)
      }
    },
    add () {
      this.$router.push('/systemSettings/dictionaries/addDictionaries')
    },
    edit (row) {
      this.$router.push({
        path: '/systemSettings/dictionaries/editDictionaries',
        query: { code: row.code }
      })
    },
    delUser (row) {
      this.$confirm('确认删除该条数据？', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http
          .post(`/${this.$const.PREFIX.AUTHORITY}/dictionary/dels`, {
            codes: [row]
          })
          .then(res => {
            if (res.result) {
              this.queryDataForm.page = 1
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.getList()
            } else {
              this.$message.error({
                message: res.msg,
                duration: 5000
              })
            }
          })
      })
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    getList (type) {
      this.queryDataForm.name = this.queryDataForm.name.trim()
      this.queryDataForm.type = this.queryDataForm.type.trim()
      if (type && type === 'noPage') {
        this.queryDataForm.page = 1
      }
      this.listLoading = true
      this.$http
        .post(
          `/${this.$const.PREFIX.AUTHORITY}/dictionary/findAllDictionaryByConditons`,
          this.queryDataForm
        )
        .then(res => {
          var backData = res.data
          this.listLoading = false
          if (res.result) {
            this.totalPage = backData.total
            this.tableData = backData.records
          } else {
            this.$message.error({
              message: res.msg,
              duration: 5000
            })
          }
        })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.queryDataForm.size = val
      this.queryDataForm.page = 1
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.queryDataForm.page = val
      this.getList()
    },
    delQueryModel () {}
  }
}
</script>
